/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.menucomponent;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Map;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.GUIGenericPawn;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Stats;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicPanelUI;
import javax.swing.plaf.basic.BasicProgressBarUI;

/**
 * @author Massimo Canonaco
 *
 */
class DisplayLeftMenuPanel016 extends JPanel implements ItemListener
{
//	private RowPair pawnName;
	private RowPair pawnRole;
	private RowPair pawnLevel;
	private JProgressBar pawnExperience;
	private RowPair[] pawnStatics;
	private JLabel[] pawnSpells;
	private Border pairBorder;
	
	DisplayLeftMenuPanel016(int statics, int spells, final Map<String, String> settings) {
//	DisplayLeftMenuPanel016(final Map<String, String> settings) {
		super();
		BoxLayout layout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
		setLayout(layout);
		final OverallGUILayoutFactory guiFactory = OverallGUILayoutFactory.getInstance();
		setOpaque(false);
		SettingsBean settingsBean = new SettingsBean();
		settingsBean.labelFont = guiFactory.getParagraphFont().deriveFont(Float.parseFloat(settings.get("pm.leftmenu.text.size")));
		settingsBean.labelColor = guiFactory.getParsedColor(settings.get("pm.leftmenu.text.color"));
		pairBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, guiFactory.getParsedColor(settings.get("pm.leftmenu.dsp.bordercolor")));
		
//		pawnName = new RowPair();
//		this.setRowPair(pawnName, settingsBean, "Nome", "");
		JPanel namePanel = new JPanel();
//		this.addRowPair(pawnName, namePanel);
		namePanel.setBackground(guiFactory.getParsedColor(settings.get("pm.leftmenu.dsp.nbackcolor")));
		namePanel.setOpaque(true);

		pawnRole = new RowPair();
		
		this.setRowPair(pawnRole, settingsBean, "Ruolo", "");
		this.addRowPair(pawnRole, new JPanel());
		pawnLevel = new RowPair();
		this.setRowPair(pawnLevel, settingsBean, "Livello", "");
		this.addRowPair(pawnLevel, new JPanel());
		
		pawnExperience = new JProgressBar(0, 100);
		pawnExperience.setString("experience");
		pawnExperience.setStringPainted(true);
		pawnExperience.setBackground(guiFactory.getParsedColor(settings.get("pm.leftmenu.progressbar.incompletecolor")));
		pawnExperience.setForeground(guiFactory.getParsedColor(settings.get("pm.leftmenu.progressbar.completecolor")));
		pawnExperience.setUI(new BasicProgressBarUI(){
			@Override
			protected Color getSelectionBackground() 
			{
				return guiFactory.getParsedColor(settings.get("pm.leftmenu.progressbar.text.color"));
			}
			@Override
			protected Color getSelectionForeground() 
			{
				return getSelectionBackground();
			}
		});
		pawnExperience.setBorder(BorderFactory.createEmptyBorder());
		pawnExperience.setFont(guiFactory.getHeaderFont().deriveFont(Float.parseFloat(settings.get("pm.leftmenu.progressbar.text.size"))));
		pawnExperience.setVisible(true);
		pawnExperience.setValue(0);
		add(pawnExperience);
		pawnStatics = new RowPair[statics];
		pawnSpells = new JLabel[spells];
		boolean boolStatics = true, boolSpells = true;
		int index = 0;
		final JPanel spellsPanel = new JPanel();
		spellsPanel.setLayout(new BoxLayout(spellsPanel, BoxLayout.PAGE_AXIS));
		spellsPanel.setOpaque(false);
		while(boolStatics || boolSpells)
		{
			boolStatics = (index < statics) ? true : false;
			boolSpells = (index < spells) ? true : false;
			
			if(boolStatics)
			{
				pawnStatics[index] = new RowPair();
				this.setRowPair(pawnStatics[index], settingsBean, Stats.statsKeyString(index), "");
				this.addRowPair(pawnStatics[index], new JPanel());
			}
			
			if(boolSpells)
			{
				pawnSpells[index] = new JLabel();
				setLabel(pawnSpells[index], settingsBean);
				pawnSpells[index].setText("");
				spellsPanel.add(pawnSpells[index]);
			}
			
			++index;
		}
		final JPanel fixPanel = new JPanel();
		fixPanel.add(spellsPanel);
		fixPanel.add(Box.createGlue());
		add(fixPanel);
		fixPanel.setOpaque(false);
	}
	
	void setInformation(GUIGenericPawn pawn)
	{
//		pawnName.labelValue.setText(pawn.getLogicPart().getName());
	}
	
	private void settingText(RowPair pair, final String textOne, final String textTwo)
	{
		pair.labelName.setText(textOne);
		pair.labelValue.setText(textTwo);
	}
	
	private void setRowPair(RowPair row, SettingsBean settings, String textName, String textValue)
	{
		setLabel(row.labelName, settings);
		setLabel(row.labelValue, settings);
		settingText(row, textName, textValue);
	}
	
	private void setLabel(final JLabel label, SettingsBean settings)
	{
		label.setFont(settings.labelFont);
		label.setForeground(settings.labelColor);
	}
	
	private void addRowPair(RowPair row, JPanel rowPairPanel)
	{
		rowPairPanel.setLayout(new BoxLayout(rowPairPanel, BoxLayout.X_AXIS));
		rowPairPanel.setOpaque(false);
		rowPairPanel.add(row.labelName);
		rowPairPanel.add(Box.createHorizontalGlue());
		rowPairPanel.add(row.labelValue);
		rowPairPanel.setBorder(pairBorder);
		add(rowPairPanel);
	}
	
	/** */
	private class RowPair{
		private JLabel labelName;
		private JLabel labelValue;
		
		private RowPair()
		{
			labelName = new JLabel();
			labelValue = new JLabel();
		}
	}
	
	/** */
	private class SettingsBean
	{
		private Font labelFont;
		private Color labelColor;
	}

	@Override
	public void itemStateChanged(ItemEvent item) 
	{
		if(item.getStateChange() == ItemEvent.SELECTED)
		{
			GUIGenericPawn selected = (GUIGenericPawn)item.getItem();
//			pawnName.labelValue.setText(selected.getLogicPart().getName());
			
			Character character = (Character)selected.getLogicPart();
			pawnRole.labelValue.setText(character.getStats().getName());
			pawnLevel.labelValue.setText(Integer.toString(character.getLevel()));
			pawnExperience.setValue((int)character.getProgressExperience());
			
			int statics = Stats.TOTAL_STATISTICS, spells = character.getStats().getAbilities().size();
			
			boolean boolStatics = true, boolSpells = true;
			int index = 0;
			while(boolStatics || boolSpells)
			{
				boolStatics = (index < statics) ? true : false;
				boolSpells = (index < spells) ? true : false;
				
				if(boolStatics)
				{
					pawnStatics[index].labelValue.setText(Float.toString(character.getStats().getCharacteristics().getStats(index)));
				}
				
				if(boolSpells)
				{
					pawnSpells[index].setText(character.getStats().getAbilities().get(index).getAbilityValue().getName());
				}
				
				++index;
			}
		}
		
	}
}
